/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeyman.securesave.action;

import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import com.ekeyman.securesave.security.EkeymanUserDetails;
import com.ekeyman.securesavelib.business.ResourceManager;
import com.ekeyman.securesavelib.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class EditResourceAction extends ActionSupport implements Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5404639506220627320L;
	private static final Log  log = LogFactory.getLog(EditResourceAction.class);
	private ResourceManager resourceManager;
	private String oper = "edit";
	private User user;
	
	private String id;
	private String application;
	private String device;
	private String resourceUri;

	/*
	 * Extracts the OpenID url from EkeymanUserDetails, then finds 
	 * the user associated with the OpenID url. The apiKey is set
	 * from the vendor associated with the user.
	 */	 
	public void prepare() throws Exception {
		EkeymanUserDetails ekeymanUserDetails = 
			(EkeymanUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		User user = resourceManager.getUser(ekeymanUserDetails.getUsername());
		if(user!= null){
			setUser(user);
		}
		
	}

	
	/*
	 * Determines which edit operation is being requested.
	 * Add and Edit are not used as resources can only be added through the
	 * web service. Del deletes the resource.
	 */
	public String execute() throws Exception {
		String result = NONE;
		
		try{
			if (oper.equalsIgnoreCase("add")){
				// Not used...Upload action will create resource
				// Also needs to save to bucket
			}
			else if (oper.equalsIgnoreCase("edit")) {
				// Not used...Resources are immutable, they can only be added 
				// or deleted 				
			}
			else if (oper.equalsIgnoreCase("del")){
		        StringTokenizer ids = new StringTokenizer(id, ",");
		        while (ids.hasMoreTokens())
		        {
		          int removeId = Integer.parseInt(ids.nextToken());
		          resourceManager.deleteResourceById(removeId, getUser());
		        }
			}
		}
		catch(Exception e){
		      addActionError("ERROR : " + e.getLocalizedMessage());
		      result = ERROR;
		}
		return result;
	}

	/*
	 * BE CAREFUL! - When the action is serialized as a JSON object all getters are
	 * executed. While traversing the object graph for resourceManager, JSON will encounter
	 * objects (i.e. Hibernate object) that cannot be serialized. The exception will go
	 * undetected as JQGrid has no way to report it!
	 * 
	public ResourceManager getResourceManager() {
		return resourceManager;
	}
	*/

	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String getOper() {
		return oper;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public User getUser() {
		return user;
	}


	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setApplication(String application) {
		this.application = application;
	}

	public String getApplication() {
		return application;
	}

	public void setDevice(String device) {
		this.device = device;
	}

	public String getDevice() {
		return device;
	}

	public void setResourceUri(String resourceUri) {
		this.resourceUri = resourceUri;
	}

	public String getResourceUri() {
		return resourceUri;
	}
}
